﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.ProductBonus.Business;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;

namespace Dashboard.ProductBonus
{
    public partial class FormAttendanceDiffReport : Form
    {
        public FormAttendanceDiffReport()
        {
            InitializeComponent();
        }

        private void FormAttendanceDiffReport_Load(object sender, EventArgs e)
        {
            this.SetDefaultDateFromTo();
            this.BindTeamList();
        }

        private void comboBoxTeam_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindShift();
        }

        private void comboBoxShift_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindStaffNoList();
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                AttendanceReport ar = new AttendanceReport();

                DataTable result = ar.GetAttendanceDiffReport(this.dateTimePickerFrom.Value.Date, 
                                                              this.dateTimePickerTo.Value.Date, 
                                                              this.comboBoxTeam.Text, 
                                                              this.comboBoxShift.Text, 
                                                              (this.comboBoxStaffNo.Text == "" ? "" : this.comboBoxStaffNo.Text.Split('-')[0]),
                                                              this.checkBoxShowDiff.Checked);

                this.ultraGridMain.DataSource = result;
                this.ultraGridMain.DataBind();

                if (this.ultraGridMain.Rows.Count > 0)
                {
                    this.toolStripButtonExport.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            var dialogResult = this.saveFileDialogMain.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                var fileName = this.saveFileDialogMain.FileName;
                this.ultraGridExcelExporterMain.Export(this.ultraGridMain, fileName);
                MessageBox.Show("Export data successfully!", "Information", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BindTeamList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable teams = bb.LoadTeamList();

            this.comboBoxTeam.Items.Clear();
            this.comboBoxTeam.Items.Add("");

            foreach (DataRow row in teams.Rows)
            {
                this.comboBoxTeam.Items.Add(row["team"].ToString());
            }

            if (this.comboBoxTeam.Items.Count > 0)
            {
                this.comboBoxTeam.SelectedIndex = 0;
            }

            bb = null;
        }

        private void BindShift()
        {
            BusinessBase bb = new BusinessBase();
            DataTable result = bb.LoadTeamShiftList(this.comboBoxTeam.Text);

            this.comboBoxShift.Items.Clear();
            this.comboBoxShift.Items.Add("");

            foreach (DataRow row in result.Rows)
            {
                this.comboBoxShift.Items.Add(row["Shift"].ToString());
            }
            if (this.comboBoxShift.Items.Count > 0)
            {
                this.comboBoxShift.SelectedIndex = 0;
            }
        }

        private void BindStaffNoList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable staffs = bb.GetStaffOfTeamShift(this.comboBoxTeam.Text, this.comboBoxShift.Text);

            this.comboBoxStaffNo.Items.Clear();
            this.comboBoxStaffNo.Text = string.Empty;
            this.comboBoxStaffNo.Items.Add("");

            foreach (DataRow row in staffs.Rows)
            {
                this.comboBoxStaffNo.Items.Add(row["staffno"].ToString() + "-" + row["name"].ToString());
            }
            if (this.comboBoxStaffNo.Items.Count > 0)
            {
                this.comboBoxStaffNo.SelectedIndex = 0;
            }
            bb = null;
        }

        private void SetDefaultDateFromTo()
        {
            var today = DateTime.Today.Date;
            var monday = today.AddDays(-((int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek)).AddDays(1);
            this.dateTimePickerFrom.Value = monday;
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除\更新
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;
            e.Layout.Override.AllowUpdate = DefaultableBoolean.False;

            e.Layout.Override.CellClickAction = CellClickAction.RowSelect;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "StaffNo";

            e.Layout.Bands[0].Columns["WorkingShiftID"].Hidden = true;
            e.Layout.Bands[0].Columns["Onduty"].Hidden = true;
        }

        private void ultraGridMain_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
        {
            if (e.Row.IsDataRow)
            {
                string staffNo = e.Row.Cells["StaffNo"].Value.ToString();
                DateTime date = Convert.ToDateTime(e.Row.Cells["Date"].Value);

                using (FormTimeCardList ftcl = new FormTimeCardList())
                {
                    ftcl.StaffNo = staffNo;
                    ftcl.Date = date;

                    ftcl.ShowDialog();
                }
            }
        }
    }
}
